#coding:utf-8

import wx
import subject.notifier as notify

class NoteBook_HasApk(wx.Panel):
    def __init__(self,parent,id):
        wx.Panel.__init__(self,parent,id)
        self.uiData = HasApk_Data()
        ui = self.CreateUI()
        self.SetSizer(ui)
        ui.SetSizeHints(parent)
        ui.Fit(parent)

        subject = notify.Notifier()
        subject.addObserver(self.rawAppPath.SetValue)
        self.uiData.bindSubjectToRawAppPtah(subject)

        subject = notify.Notifier()
        subject.addObserver(self.svPathAppPackaged.SetValue)
        self.uiData.bindSubjectToSvAppPtah(subject)



    def CreateUI(self):

        #原始包路径
        self.rawAppPath = wx.TextCtrl(self,-1,'',size=(200,20))
        chsPath = wx.Button(self,-1,u'样包路径')

        #打包后存放路径
        self.svPathAppPackaged = wx.TextCtrl(self,-1,'',size=(200,20))
        chsSvPath = wx.Button(self,-1,u'保存路径')

        #布局
        mainsizer = wx.BoxSizer(wx.VERTICAL)

        #原始包路径
        subsizer = wx.FlexGridSizer(1,0)
        subsizer.Add(self.rawAppPath,1,wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.TOP,10)
        subsizer.Add(chsPath,1,wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.TOP|wx.LEFT,10)
        mainsizer.Add(subsizer,0,wx.EXPAND|wx.LEFT|wx.RIGHT,10)

        #打包后存放路径
        subsizer = wx.FlexGridSizer(1,0)
        subsizer.Add(self.svPathAppPackaged,1,wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.TOP,10)
        subsizer.Add(chsSvPath,1,wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.TOP|wx.LEFT,10)
        mainsizer.Add(subsizer,0,wx.EXPAND|wx.LEFT|wx.RIGHT,10)

        #绑定事件
        chsPath.Bind(wx.EVT_BUTTON,self.OnChsRawApkPath)
        chsSvPath.Bind(wx.EVT_BUTTON,self.OnChsSaveApkPackaged)
        self.rawAppPath.Bind(wx.EVT_TEXT,self.OnChsRawApkPathByText)
        self.svPathAppPackaged.Bind(wx.EVT_TEXT,self.OnChsSaveApkPackagedByText)

        return mainsizer



    def OnChsRawApkPath(self,evt):
        fileDialog = wx.FileDialog(self,u'选择样包')
        fileDialog.ShowModal()
        self.uiData.setRawAppPath(fileDialog.GetPath())

    def OnChsSaveApkPackaged(self,evt):
        fileDialog = wx.DirDialog(self,u'保存路径')
        fileDialog.ShowModal()
        self.uiData.setSvAppPath(fileDialog.GetPath())

    def OnChsRawApkPathByText(self,evt):
        self.uiData.setRawAppPathOnly(self.rawAppPath.GetValue())

    def OnChsSaveApkPackagedByText(self,evt):
        self.uiData.setSvAppPathOnly(self.svPathAppPackaged.GetValue())

class HasApk_Data:
    def __init__(self):
        self.__rawAppPath = None
        self.__svAppPath = None

    def setRawAppPath(self,data):
        self.__rawAppPath = data
        self.__rawAppPathSubject.notify(self.__rawAppPath)

    def setSvAppPath(self,data):
        self.__svAppPath = data
        self.__svAppPathSubject.notify(self.__svAppPath)
    def getRawAppPath(self):
        return self.__rawAppPath

    def getSvAppPath(self):
        return self.__svAppPath

    def setRawAppPathOnly(self,data):
        self.__rawAppPath = data

    def setSvAppPathOnly(self,data):
        self.__svAppPath = data

    def bindSubjectToRawAppPtah(self,subject):
        self.__rawAppPathSubject = subject

    def bindSubjectToSvAppPtah(self,subject):
        self.__svAppPathSubject =  subject